//#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<stack>
using namespace std;

int n, m;
vector<int> nexts[10001];
int dfn[10001] = { 0 }, low[10001] = { 0 }, tot = 0;
int vis[10001] = { 0 };
int jilu[10001] = { 0 };
stack<int> s;
int result = 0;

void input() {
	cin >> n >> m;
	int a, b;
	for (int i = 1; i <= m; i++) {
		cin >> a >> b;
		nexts[a].push_back(b);
	}
}

void tarjan(int x) {
	dfn[x] = low[x] = ++tot;
	s.push(x);
	vis[x] = 1;
	for (int i = 0; i < nexts[x].size(); i++) {
		int v = nexts[x][i];
		if (!dfn[v]) {
			tarjan(v);
			low[x] = min(low[x], low[v]);
		}
		else if (vis[v]) {
			low[x] = min(dfn[v], low[x]);
		}
	}

	if (low[x] == dfn[x]) {
		int count = 0;
		while (true) {
			int u = s.top();
			s.pop();
			vis[u] = 0;
			jilu[u] = 1;
			count++;
			if (u == x) {
				break;
			}
		}
		result += count * (count - 1) / 2;
	}
}

int main() {
	input();
	for (int i = 1; i <= n; i++) {
		if (!jilu[i]) {
			tarjan(i);
		}
	}
	cout << result << endl;
	return 0;
}